package net.vonkannen.callduration;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

public class Lay_Settings extends Activity implements OnClickListener {
	private static final String PREFS_NAME = "CallDuration_prefs";

	EditText edit_radius;
	EditText edit_borderthickness; 
	EditText edit_bordercolor;
	EditText edit_bgcolor;
	EditText edit_fontcolor;
	CheckBox chk_icon;
	
	/* Toast */
	Toast test_toast;
	GradientDrawable shape_toast;
	LinearLayout lay_toast;
	ImageView img_toast;
	TextView tv_toast;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        
        /* Get LinearLayout */
        LinearLayout layout = (LinearLayout)findViewById(R.id.lay_settings);
        
        /* Set focus to LinearLayout to prevent opening softkeyboard */
        layout.setFocusableInTouchMode(true);
        layout.requestFocus();
        
        /* Create ads */
        AdView adView = new AdView(this, AdSize.BANNER, "a14dbc9a647203e");
        adView.loadAd(new AdRequest());
        layout.addView(adView);
        
        /* OnClickListener for buttons */
        Button btn_standard = (Button)findViewById(R.id.btn_standard);
        btn_standard.setOnClickListener(this);
        Button btn_preview = (Button)findViewById(R.id.btn_preview);
        btn_preview.setOnClickListener(this);

        
        /* Get inputs */
        edit_radius = (EditText)findViewById(R.id.edit_radius);
        edit_borderthickness = (EditText)findViewById(R.id.edit_borderthickness);
        edit_bordercolor = (EditText)findViewById(R.id.edit_bordercolor);
        edit_bgcolor = (EditText)findViewById(R.id.edit_bgcolor);
        edit_fontcolor = (EditText)findViewById(R.id.edit_fontcolor);
        chk_icon = (CheckBox)findViewById(R.id.chk_icon);
        
        /* Read SharedPreferences */
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        
        /* Set preferences */
        edit_radius.setText(settings.getString("radius", "4"));
        edit_borderthickness.setText(settings.getString("borderthickness", "6"));
        edit_bordercolor.setText(settings.getString("bordercolor", "FFABABAB"));
        edit_bgcolor.setText(settings.getString("bgcolor", "FA444444"));
        edit_fontcolor.setText(settings.getString("fontcolor", "FFFFFFFF"));
        chk_icon.setChecked(settings.getBoolean("showicon", true));
    }
	
    @Override
    public void onPause() {
    	super.onPause();
	    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
	    SharedPreferences.Editor editor = settings.edit();

	    editor.putString("radius", edit_radius.getText().toString());
	    editor.putString("borderthickness", edit_borderthickness.getText().toString());
	    
	    String wrongColor="";
	    if((edit_bordercolor.getText().toString().matches("[0-9a-fA-F]+")) && (edit_bordercolor.getText().toString().length()<=8)) {
	    	editor.putString("bordercolor", edit_bordercolor.getText().toString());	
	    }
	    else {
	    	wrongColor = wrongColor.concat(getString(R.string.settings_bordercolor) + " " + getString(R.string.settings_wrongcolor));
	    }
	    if((edit_bgcolor.getText().toString().matches("[0-9a-fA-F]+")) && (edit_bgcolor.getText().toString().length()<=8)) {
	    	editor.putString("bgcolor", edit_bgcolor.getText().toString());
	    }
	    else {
	    	if(!wrongColor.equals(""))
	    		wrongColor = wrongColor.concat("\n");
    		wrongColor = wrongColor.concat(getString(R.string.settings_backgroundcolor) + " " + getString(R.string.settings_wrongcolor));
	    }
	    if((edit_fontcolor.getText().toString().matches("[0-9a-fA-F]+")) && (edit_fontcolor.getText().toString().length()<=8)) {
	    	editor.putString("fontcolor", edit_fontcolor.getText().toString());
	    }
	    else {
	    	if(!wrongColor.equals(""))
	    		wrongColor = wrongColor.concat("\n");
	    	wrongColor = wrongColor.concat(getString(R.string.settings_fontcolor) + " " + getString(R.string.settings_wrongcolor));
	    }
	    if(!wrongColor.equals(""))
	    	Toast.makeText(getApplicationContext(), wrongColor, Toast.LENGTH_LONG).show();
	    
	    editor.putBoolean("showicon", chk_icon.isChecked());
	    /* Commit the edits */
	    editor.commit();
	    this.finish();
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.btn_standard:
	        edit_radius.setText("4");
	        edit_borderthickness.setText("6");
	        edit_bordercolor.setText("FFABABAB");
	        edit_bgcolor.setText("FA444444");
	        edit_fontcolor.setText("FFFFFFFF");
	        chk_icon.setChecked(true);
			break;
		case R.id.btn_preview:
	        /* Create and show preview toast */
		    if((!edit_bordercolor.getText().toString().matches("[0-9a-fA-F]+")) || (!edit_bgcolor.getText().toString().matches("[0-9a-fA-F]+")) || (!edit_fontcolor.getText().toString().matches("[0-9a-fA-F]+"))) {
		    	Toast.makeText(getApplicationContext(), R.string.settings_wrongcolor, Toast.LENGTH_LONG).show();
		    }
		    else {
				CustomToast preview_toast = new CustomToast(getApplicationContext(),
						"Dauer: 00:13:37",
						Integer.parseInt(edit_radius.getText().toString()),
						Integer.parseInt(edit_borderthickness.getText().toString()),
						(int) Long.parseLong(edit_bordercolor.getText().toString(), 16),
						(int) Long.parseLong(edit_bgcolor.getText().toString(), 16),
						(int) Long.parseLong(edit_fontcolor.getText().toString(), 16),
						 chk_icon.isChecked());
				preview_toast.show();
		    }
			break;
		}
	}
	

}
